const User = require('../models/userModel')
const Role = require('../models/roleModel')
const { DEFAULT_PASSWORD } = require('../project.config')
const { processToken } = require('../utils/token')
const { verify } = require('../utils/token')

class UserController {
    static async create (ctx) {
        const { account, password, roleId } = ctx.request.body || {}
        const temp = await Role.findOne({
            _id: roleId,
        })
        if (!temp) {
            ctx.body = {
                code: 0,
                msg: '出错了',
            }
            return
        }
        if (account === '' || password === '') {
            ctx.body = {
                code: 0,
                msg: '字段不能为空',
                data: null,
            }
            return
        }
        const user = new User({
            account,
            password,
            roleId,
        })
        const res = await user.save()
        ctx.body = {
            code: 1,
            msg: '添加成功',
            data: res,
        }
    }

    static async remove (ctx) {
        let { id } = ctx.params
        const res = await User.deleteOne({ _id: id })

        if (!res) {
            ctx.body = {
                code: 0,
                msg: '删除失败',
            }
            return
        }

        ctx.body = {
            code: 1,
            msg: '该账户已删除',
            data: res,
        }
    }

    static async resetPassword (ctx) {
        const { id } = ctx.request.body
        const user = await User.findOne({
            _id: id,
        }).exec()
        if (!user) {
            ctx.body = {
                code: 0,
                msg: '找不到用户',
            }
            return
        }
        user.password = DEFAULT_PASSWORD
        const res = await user.save()
        ctx.body = {
            code: 1,
            msg: '修改成功',
            data: {
                _id: res._id,
                account: res.account,
            },
        }
    }

    static async findList (ctx) {
        let { currentPage, pageSize, keyword } = ctx.query
        currentPage = Number(currentPage)
        pageSize = Number(pageSize)
        const query = {}
        if (keyword) {
            query.account = keyword
        }
        const list = await User.find(query)
                               .sort({ _id: -1 })
                               .skip((currentPage - 1) * pageSize)
                               .limit(pageSize)
                               .exec()

        const total = await User.countDocuments().exec()
        ctx.body = {
            code: 1,
            msg: '获取列表成功',
            data: {
                list,
                total,
                currentPage,
                pageSize,
            },
        }
    }

    static async findUser (ctx) {
        ctx.body = {
            code: 1,
            data: await verify(processToken(ctx)), //TODO: 通过对token解密，来获得用户信息
            msg: '获取成功',
        }
    }

    static async updatePassword (ctx) {
        const { newPassword, oldPassword } = ctx.request.body

        const payload = await verify(processToken(ctx))
        const { id } = payload
        const user = await User.findOne({ _id: id }).exec()
        if (!user) {
            ctx.body = {
                msg: '用户不存在',
                code: 0,
            }
            return
        }

        if (user.password !== oldPassword) {
            ctx.body = {
                code: 0,
                msg: '原密码错误',
            }
            return
        }

        user.password = newPassword

        await user.save()

        ctx.body = {
            code: 1,
            msg: '密码修改成功',
        }
    }
}

module.exports = UserController
